package com.erp.order.controller;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.order.api.domain.ErpSellerChildOrder;
import com.ruoyi.order.api.domain.dto.EvaluationDto;
import com.alibaba.nacos.shaded.com.google.gson.JsonObject;
import com.erp.order.service.IErpSellerChildOrderService;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.web.oldController.BaseController;
import com.ruoyi.order.api.domain.ErpSellerChildOrder;
import com.ruoyi.order.api.domain.dto.EvaluationDto;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 商户子订单
 *
 * @author erp
 * @date 2023-03-28
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/order/storeChildOrder")
public class ErpSellerChildOrderController extends BaseController {

    private final IErpSellerChildOrderService iErpStoreChildOrderService;
    
    /**
     * 根据订单号获取子订单
     */
    @GetMapping("/getChildByOrder/{orderId}")
    public List<ErpSellerChildOrder> getChildByOrder(@PathVariable("orderId") Long orderId) {
        return iErpStoreChildOrderService.getChildByOrder(orderId);
    }

    /**
     * 根据子订单号获取子订单信息
     * @param childOrderId
     * @return
     */
    @GetMapping("/getChildOrderByOrderId")
    public R<Object> getChildOrderByOrderId(String childOrderId){
        ErpSellerChildOrder childOrder = iErpStoreChildOrderService.getChildOrderByOrderId(childOrderId);
        if (childOrder != null){
            return R.ok(childOrder);
        }
        return R.fail("未查询到子订单信息");
    }
    @GetMapping("/getChildByChildId/{childOrderId}")
    public ErpSellerChildOrder getChildByChildId(@PathVariable("childOrderId") String childOrderId) {
        return iErpStoreChildOrderService.getChildOrderByOrderId(childOrderId);
    }

    /**
     * 根据店铺cn码查看过去几天是否有订单
     * @param beforeDay
     * @param storeUserName
     * @return
     */
    @GetMapping("/getChildOrdersByBeforeDays")
    public R<Object> getChildOrdersByBeforeDays(@RequestParam(value = "beforeDay", required = false) Integer beforeDay,
                                                @RequestParam("storeUserName") String storeUserName,
                                                @RequestParam(value = "shieldProductIds", required = false) String shieldProductIds){
        List<String> shieldProductIdList = new ArrayList<>();
        if(shieldProductIds != null && !shieldProductIds.equals("")){
            shieldProductIdList = Arrays.stream(shieldProductIds.split(",")).collect(Collectors.toList());
        }
        List<ErpSellerChildOrder> orderList = iErpStoreChildOrderService.getOrdersByDay(beforeDay, storeUserName, shieldProductIdList);
        if (orderList != null && orderList.size() > 0){
            return R.ok(orderList);
        }
        return R.fail("暂无订单");
    }

    /**
     * 根据类型获取店铺运营数据
     * @param productId
     * @param dataType
     * @return
     */
    @GetMapping("/getProductOperationsData/{productId}/{dataType}")
    public R<Object> getProductOperationsData(@PathVariable("productId") String productId,
                                              @PathVariable("dataType") String dataType){
        EvaluationDto dto = iErpStoreChildOrderService.getProductOperationsData(Long.parseLong(productId), dataType);
        if (dto != null){
            return R.ok(dto);
        }
        return R.fail("暂无该商品的运营数据");
    }

    /**
     * 判断上一单是否是测评单
     * @param storeUserName
     * @param skuCode
     * @return
     */
    @GetMapping("/getIsEvaluationBySkuCode/{storeUserName}/{skuCode}")
    public R<Object> getIsEvaluationBySkuCode(@PathVariable("storeUserName") String storeUserName,
                                              @PathVariable("skuCode") String skuCode){
        return iErpStoreChildOrderService.selectIsEvaluationBySkuCode(storeUserName, skuCode) == 1 ? R.ok() : R.fail();
    }

    /**
     * 判断商品是否出单
     * @param productIds 商品id
     * @param moduleReplace 前几天
     * @return 出单量
     */
    @GetMapping("/isOrderPlacedForProduct")
    public Boolean isOrderPlacedForProduct(@RequestParam("productIds") String productIds,@RequestParam(value = "moduleReplace",required = false) Integer moduleReplace) {
        return iErpStoreChildOrderService.isOrderPlacedForProduct(productIds, moduleReplace);
    }

    /**
     * 获取订单量和违规率
     */
    @GetMapping("/getOrderCountByCode")
    public R<Object> getOrderCountByCode(String skuCode) {
        Map<String, Object> countByCode = iErpStoreChildOrderService.getOrderCountByCode(skuCode);
        return R.ok(countByCode);
    }
//
//    /**
//     * 查询商户子订单列表
//     */
//    @SaCheckPermission("purchase:storeChildOrder:list")
//    @GetMapping("/list")
//    public TableDataInfo<ErpSellerChildOrderVo> list(ErpSellerChildOrderBo bo, PageQuery pageQuery) {
//        return iErpStoreChildOrderService.queryPageList(bo, pageQuery);
//    }
//
//    /**
//     * 导出商户子订单列表
//     */
//    @SaCheckPermission("purchase:storeChildOrder:export")
//    @Log(title = "商户子订单", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    public void export(ErpSellerChildOrderBo bo, HttpServletResponse response) {
//        List<ErpSellerChildOrderVo> list = iErpStoreChildOrderService.queryList(bo);
//        ExcelUtil.exportExcel(list, "商户子订单", ErpSellerChildOrderVo.class, response);
//    }
//
//    /**
//     * 获取商户子订单详细信息
//     *
//     * @param id 主键
//     */
//    @SaCheckPermission("purchase:storeChildOrder:query")
//    @GetMapping("/{id}")
//    public R<ErpSellerChildOrderVo> getInfo(@NotNull(message = "主键不能为空")
//                                     @PathVariable Long id) {
//        return R.ok(iErpStoreChildOrderService.queryById(id));
//    }
//
//    /**
//     * 新增商户子订单
//     */
//    @SaCheckPermission("purchase:storeChildOrder:add")
//    @Log(title = "商户子订单", businessType = BusinessType.INSERT)
//    @RepeatSubmit()
//    @PostMapping()
//    public R<Void> add(@Validated(AddGroup.class) @RequestBody ErpSellerChildOrderBo bo) {
//        return toAjax(iErpStoreChildOrderService.insertByBo(bo));
//    }
//
//    /**
//     * 修改商户子订单
//     */
//    @SaCheckPermission("purchase:storeChildOrder:edit")
//    @Log(title = "商户子订单", businessType = BusinessType.UPDATE)
//    @RepeatSubmit()
//    @PutMapping()
//    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ErpSellerChildOrderBo bo) {
//        return toAjax(iErpStoreChildOrderService.updateByBo(bo));
//    }
//
//    /**
//     * 删除商户子订单
//     *
//     * @param ids 主键串
//     */
//    @SaCheckPermission("purchase:storeChildOrder:remove")
//    @Log(title = "商户子订单", businessType = BusinessType.DELETE)
//    @DeleteMapping("/{ids}")
//    public R<Void> remove(@NotEmpty(message = "主键不能为空")
//                          @PathVariable Long[] ids) {
//        return toAjax(iErpStoreChildOrderService.deleteWithValidByIds(Arrays.asList(ids), true));
//    }
}
